using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace TicTacToe.GC
{
    public delegate void OnPressedHandler(object sender);
    public delegate void OnReleasedHandler(object sender);

    public class GameControl
    {
        protected bool isInit;
        protected int xCoord;
        protected int yCoord;
        protected string textureDirectory;

        protected Texture2D texture;
        protected Rectangle rectangle;

        public event OnPressedHandler OnPressedEvent;
        public event OnReleasedHandler OnReleasedEvent;

        public GameControl(string directory, int x, int y)
        {
            this.textureDirectory = directory;
            this.xCoord = x;
            this.yCoord = y;
            this.isInit = false;
        }

        public void OnPressed()
        {
            if (OnPressedEvent != null)
                OnPressedEvent(this);
        }

        public void OnReleased()
        {
            if (OnReleasedEvent != null)
                OnReleasedEvent(this);
        }

        public void Init(ContentManager cm)
        {
            texture = cm.Load<Texture2D>(textureDirectory);
            rectangle = new Rectangle(xCoord, yCoord, texture.Width, texture.Height);
            isInit = true;
        }

        public void Draw(SpriteBatch sprite, Color color)
        {
            sprite.Draw(texture, rectangle, color);
        }

        public bool IsContain(int x, int y)
        {
            return rectangle.Contains(x, y);
        }

        public void ReloadControl(string directory, int x, int y)
        {
            this.textureDirectory = directory;
            this.xCoord = x;
            this.yCoord = y;
            this.isInit = false;
        }

        public void ReloadControl(string directory)
        {
            this.textureDirectory = directory;
            this.isInit = false;
        }

        public void ReloadControl(int x, int y)
        {
            this.xCoord = x;
            this.yCoord = y;
            this.isInit = false;
        }

        public bool IsInit() { return isInit; }
    }
}
